<?php
header("Content-type:text/html;Charset=utf-8");

//被装饰者基类  #抽象构件
interface Component{
    function operation();
}
//被装饰者类   #具体构件
class ConcreteComponent implements Component{
    function operation(){
        echo "加了被装饰者";
    }
}
//装饰者基类  #抽象装饰类
abstract class Decorator implements Component{
    private $component;
    function __construct($component){
        $this->component = $component;
    }
    //实现接口方法
    function operation(){
        $this->component->operation();
    }
}
//具体装饰类A   #具体装饰类
class ConcreteDecoratorA extends Decorator {
    public function __construct(Component $component) {
        parent::__construct($component);
    }

    public function operation() {
        parent::operation();
        $this->addedOperationA();   //  新增加的操作
    }

    public function addedOperationA() {
        echo "，又加了A个性化装饰<br>";
    }
}
//具体装饰类B
class ConcreteDecoratorB extends Decorator {
    public function __construct(Component $component) {
        parent::__construct($component);

    }

    public function operation() {
        parent::operation();
        $this->addedOperationB();   //  新增加的操作
    }

    public function addedOperationB() {
        echo "，又加了B个性化装饰<br><br>";
    }
}

//测试
$decoratorA = new ConcreteDecoratorA(new ConcreteComponent());
$decoratorA->operation();
$decoratorB = new ConcreteDecoratorB($decoratorA);
$decoratorB->operation();
/*
 加了被装饰者，又加了A个性化装饰
 加了被装饰者，又加了A个性化装饰
 ，又加了B个性化装饰
*/

